package com.linkwings.projectsimple.http.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.linkwings.projectsimple.pojo.SampleUser;
import com.linkwings.projectsimple.pojo.UserInfo;
import com.linkwings.projectsimple.service.UserService;


@Controller
@RequestMapping("/user")
public class UserController {

		private UserService userService=null;

	   @RequestMapping(value="/index", method=RequestMethod.GET)
	   public String index(HttpServletRequest request){
		 List<SampleUser> list = userService.findListUser();
		 request.setAttribute("list", list);
	     return "/user/list";
	   }




	   /*common param and path var param*/
	   @RequestMapping(value="/{id}/show",  method=RequestMethod.GET)
	   public String show(@PathVariable int id,HttpServletRequest request){

		 SampleUser user = userService.findUserById(id);
		 request.setAttribute("user", user);
	     return "/user/view";
	   }


	   /*common param and path var param*/
	   @RequestMapping(value="/{id}/edit",  method=RequestMethod.GET)
	   public String edit(@PathVariable int id,HttpServletRequest request){
		 SampleUser user = userService.findUserById(id);
		 request.setAttribute("user", user);
	     return "/user/form";
	   }

	   /*common param and path var param*/
	   @RequestMapping(value="/{id}",method=RequestMethod.POST)
	   public String save(@PathVariable("id") int id,SampleUser user ,HttpServletRequest request){
		  SampleUser userOld = userService.findUserById(id);
		  userOld.setEmail(user.getEmail());
		  userOld.setNickName(user.getNickName());
		  userOld.setPassword(user.getPassword());
		  userService.saveOrUpdateUser(userOld);
		 return"redirect:/user/index";
	   }

	public void setUserService(UserService userService) {
		this.userService = userService;
	}
}
